package com.sinosoft.oauth2Server.erro.security;

import com.sinosoft.enums.http.StatusCode;
import com.sinosoft.vo.http.Result;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Auther: zouren
 * @Date: 2019/2/19 13:56p
 * @Description:
 */
@Component
public class AuthenticationAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse resp, AccessDeniedException e) throws IOException, ServletException {
//        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
        resp.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        Result result = Result.failure(StatusCode.FORBIDDEN);
        PrintWriter out = resp.getWriter();
        result.setDescription(e.getMessage());
        result.setPath(httpServletRequest.getServletPath());
        out.write( result.toJSONString());
    }
}

